package com.bayern.muchen.more_test.compleatable;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @author QIUYANG
 * @title: CpTest
 * @description: TODO
 * @date 2022/1/710:05
 */
public class CpTest {


    public static void main(String[] args) {
        String tagResult = "123%123;1%123;2%312;3%13;1%13";
        String[] tagArr = {};
        tagArr = tagResult.split(";");

        JSONArray phoneList = new JSONArray();
        System.out.println(tagResult);
        List<String> list = Arrays.asList(tagArr);
        CompletableFuture[] futures = new CompletableFuture[list.size()];
        int count = 0;
        for (String s : list) {
           futures[count]= CompletableFuture.runAsync(()->user(s,phoneList));
           count++;
        }
        CompletableFuture.allOf(futures).join();
        for (Object o : phoneList) {
            System.out.println(o);
        }

    }

    private static void user(String s, JSONArray phoneList) {
        System.out.println("===================="+Thread.currentThread().getId());
        JSONObject numObj = new JSONObject();
        String numVal = s.split("%")[0];
        String poolId = s.split("%")[1];

        JSONObject checkJson = checkCard(numVal,poolId);

        //JSONObject choiceJson = getGroupChoiceNum(poolId, cityCode, channelId, numVal);
        if(!"0".equals(checkJson.get("retCode"))){//该号码不可用
            return;
        }
        numObj.put("number", numVal);
        numObj.put("poolId", poolId);
        numObj.put("remark", "孪生号");
        phoneList.add(numObj);
    }

    public static JSONObject checkCard(String numVal,String poolId){
        JSONObject jsonObject = new JSONObject();

        if (numVal.contains("1")){
            jsonObject.put("retCode","0");
        }else {
            jsonObject.put("retCode","1");
        }


        return jsonObject;
    }



}
